// pages/activity/activity.js
const app = getApp();
const getToken = require('../../utils/get_token.js');
let hebiRedFlag;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getBanner(that);
  if(hebiRedFlag==1){
    newList(that);
  }
  goodList(that);
}
const getBanner = (that) => {
  let params = [ 'shop/pptIco/lorB', {"A":"QD_HJH_TB"}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data = res.data;
    data.A.QD_HJH_TB[0].show=true;
    for(let i=0;i<4;i++)
    {   
      that.data.cList.push(data.A.QD_HJH_TB[i])
    }	
    that.setData({cList: that.data.cList})
    that.data.current=data.A.QD_HJH_TB[0].E;
    indexList(that,that.data.current);
  }, ...params)
};
const newList = (that) => {//新人列表
    let params = ['shop/pingTuan/subjectValueBestGoodsList', {'pageNo': 1, 'pageSize': 10, 'isBest': 1,'subjectValue':0 }]
    getToken.getData(function (res) {
      let data = res.data;
      if(data.rows.length>0)
			{
				for(let i=0;i<data.rows.length;i++)
				{   
          data.rows[i].goodsImg='https://www.globalhexi.com/'+data.rows[i].goodsImg;
          data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
          data.rows[i].intPinPrice = (data.rows[i].intPinPrice/100).toFixed(2);
          that.data.arr.push(data.rows[i]);
				}		  	 
      } 
      that.setData({arr: that.data.arr})
    }, ...params)
  
};
const goodList = (that) => {//商品列表
  let params = ['shop/pingTuan/subjectValueBestGoodsList', {'pageNo': 1, 'pageSize': 10, 'isBest': 0,'subjectValue':108 }]
    getToken.getData(function (res) {
      let data = res.data;
      if(data.rows.length>0)
			{
				for(let i=0;i<data.rows.length;i++)
				{   
          data.rows[i].goodsImg='https://www.globalhexi.com/'+data.rows[i].goodsImg;
          data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
          data.rows[i].intPinPrice = (data.rows[i].intPinPrice/100).toFixed(2);
          that.data.arr.push(data.rows[i]);
				}		  	 
      } 
      that.setData({arr: that.data.arr})
    }, ...params)
};
const indexList = (that,id) => {//下拉商品列表
  if (that.data.boolT) {
    that.setData({hidden: false})
    that.data.boolT = false;
    let params = ['shop/pingTuan/subjectValueBestGoodsList', {'pageNo': that.data.pNo, 'pageSize': 10, 'isBest': 0,'subjectValue':id}]
    getToken.getData(function (res) {
      let data = res.data;
      console.log(res.data)
      if(that.data.pNo==1)
      {
        that.data.gList=[];
      }
      if(data.rows.length>0)
			{
				for(let i=0;i<data.rows.length;i++)
				{   
          data.rows[i].goodsImg='https://www.globalhexi.com/'+data.rows[i].goodsImg;
          data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
          data.rows[i].intPinPrice = (data.rows[i].intPinPrice/100).toFixed(2);
          that.data.gList.push(data.rows[i]);
				}
				that.data.pNo++;			  	 
			} 
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          gList: that.data.gList,
          hidden: true
        })
      }, 500)
    }, ...params)
  }
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    hidden: false,
    boolT: true,
    img:"",
    pNo:1,
    current:'',
    cList:[],
    gList:[],
    arr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    hebiRedFlag= wx.getStorageSync('hebiRedFlag') || '';
    ckToken(that);
  },
  change:function(e){
    let that = this;
    let id=e.currentTarget.dataset.id;
    if (that.data.boolT) {
      that.data.pNo=1;
      that.data.gList=[];
      that.data.current=id;
      for(let i=0;i<that.data.cList.length;i++)
      {
        if(id==that.data.cList[i].E)
        {
          that.data.cList[i].show=true;
        }else{
          that.data.cList[i].show=false;
        }
      }
      that.setData({hidden: false})
      that.data.boolT = false;
      let params = ['shop/pingTuan/subjectValueBestGoodsList', {'pageNo': that.data.pNo, 'pageSize': 10, 'isBest': 0,'subjectValue':id}]
      getToken.getData(function (res) {
        let data = res.data;
        console.log(res.data)
        if(data.rows.length>0)
        {
          for(let i=0;i<data.rows.length;i++)
          {   
            data.rows[i].goodsImg='https://www.globalhexi.com/'+data.rows[i].goodsImg;
            data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
            data.rows[i].intPinPrice = (data.rows[i].intPinPrice/100).toFixed(2);
            that.data.gList.push(data.rows[i]);
          }
          that.data.pNo++;			  	 
        } 
        that.setData({
          gList: that.data.gList,
          cList:that.data.cList
        })
        setTimeout(function () {
          that.setData({hidden: true})
          that.data.boolT = true;
        }, 300)
      }, ...params)
    }
  },

  enter: function (e) {
    let that = this;
    console.log(e.currentTarget.dataset.id)
		if (that.data.boolT) {
			that.data.boolT = false;
			wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
				success: function () {
					setTimeout(function () {
						that.data.boolT = true;
					}, 500)
				}
			})
		}
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
 

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    indexList(that,that.data.current);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})